import type { ActionType } from "@/js/constants";
export function isString(value: unknown) {
  return typeof value === "string";
}

export function setClass(el: HTMLElement, className: string) {
  el.classList.add(className);
}

export function getAction(event: Event): ActionType | undefined {
  if (!(event.target instanceof HTMLElement)) {
    return void 0;
  }

  return event.target.dataset.starAction as ActionType;
}
const REGEXP_SPACES = /\s\s*/;
export function addEventListener(
  element: HTMLElement | Document,
  type: string,
  handle: EventListenerOrEventListenerObject,
  option: boolean | AddEventListenerOptions = false
) {
  type
    .trim()
    .split(REGEXP_SPACES)
    .forEach((type) => {
      element.addEventListener(type, handle, option);
    });
}

export function removeEventListener(
  element: HTMLElement,
  type: string,
  handle: EventListenerOrEventListenerObject,
  option: boolean | AddEventListenerOptions = false
) {
  type
    .trim()
    .split(REGEXP_SPACES)
    .forEach((type) => {
      element.removeEventListener(type, handle, option);
    });
}
